<?php

	// if post
	if($_REQUEST['searchText'])
	{
		
		$searchOk = true;
	}
// ici on affiche un texte en hut de la page pour le changer c'esty dans modules/message_haut.php
echo '' . Recherche . '';
	
	// form

	$out = '<form action="">
				<label>Rechercher dans : </label> <select name="targetSearch">
													<option value="membre_forum">Forum</option>
													<option value="tutos">Tutoriel</option>
													<option value="forum_sorties">Sortie</option>
												</select>
				<label>Le mot suivant : </label> <input type="text" name="searchText" />
				<input type="submit" value="Lancer la recherche" />
			</form>';
	
	
	// search
	
	function normaliserRecherche($str)
	{
		$in = array('é','è','ê','à','ù');
		$out = array('e','e','e','a','u');
		
		return str_replace($in, $out, $str);
	}
	function getTopicTitre($topicId)
	{
		return GetValueFromQuery('SELECT topic_titre as value FROM forum_topic WHERE topic_id='.$topicId);
	}
	
	if($searchOk)
	{
		$page = null;
		switch($_REQUEST['targetSearch'])
		{

			case 'membre_forum':
				// topic
				$query = $pdo->prepare('SELECT * FROM forum_topic WHERE topic_titre LIKE "%'.$_REQUEST['searchText'].'%"');
				$query->execute();
				if($query->rowCount())
				{
					while($d = $query->fetch(PDO::FETCH_OBJ))
					{
						$page['forum'][$d->topic_id] = '<a href="forum-voirmessage.html?tid='.$d->topic_id.'">'.$d->topic_titre.'</a>';
					}
				}
				
				// post
				$query = $pdo->prepare('SELECT * FROM forum_post WHERE post_texte LIKE "%'.$_REQUEST['searchText'].'%"');
				$query->execute();
				if($query->rowCount())
				{
					while($d = $query->fetch(PDO::FETCH_OBJ))
					{
						$page['forum'][$d->topic_id] = '<a href="forum-voirmessage.html?tid='.$d->topic_id.'">'.getTopicTitre($d->topic_id).'</a>';
					}
				}
				break;
			
			case 'tutos':
				// cat
				$query = $pdo->prepare('SELECT * FROM tutos_cat WHERE tuto_cat_name LIKE "%'.$_REQUEST['searchText'].'%" OR tuto_cat_info LIKE "%'.$_REQUEST['searchText'].'%"');
				$query->execute();
				if($query->rowCount())
				{
					while($d = $query->fetch(PDO::FETCH_OBJ))
					{
						$page['tutos'][$d->tuto_cat_id] = '<a href="tuto.html?cat='.$d->tuto_cat_id.'">'.$d->tuto_cat_name.'</a>';
					}
				}
				
				// tutos
				$query = $pdo->prepare('SELECT * FROM tutos WHERE titre LIKE "%'.$_REQUEST['searchText'].'%" OR contenu LIKE "%'.$_REQUEST['searchText'].'%"');
				$query->execute();
				if($query->rowCount())
				{
					while($d = $query->fetch(PDO::FETCH_OBJ))
					{
						$page['tutos'][$d->id] = '<a href="tuto-view.html?tutoid='.$d->id.'">'.$d->titre.'</a>';
					}
				}
				break;
			
			
			case 'forum_sorties':
				$query = $pdo->prepare('SELECT * FROM activite_sortie WHERE titreSortie LIKE "%'.$_REQUEST['searchText'].'%" OR description LIKE "%'.$_REQUEST['searchText'].'%"');
				$query->execute();
				if($query->rowCount())
				{
					while($d = $query->fetch(PDO::FETCH_OBJ))
					{
						$page['sorties'][$d->sortie_activite_id] = '<a href="activites-detailSortie.html?id='.$d->sortie_activite_id.'">'.$d->titreSortie.'</a>';
					}
				}
				
				break;
				
		}
	}

	// put pages
	if(count($page))
	{
		$out .= '<hr/>';	
		foreach($page as $k=>$v)
		{
			$out .= '<b>'.$k.'</b>';
			foreach($v as $v2)
			{
				$out .= $v2;
			}			
		}
	}
	// output
	echo $out;
	
	
?>